package model

import (
	"github.com/jinzhu/gorm"
	uuid "github.com/satori/go.uuid"
	"mynetdisk/utils"
)

/**
 *@Author tudou
 *@Date 2019/8/24
 **/

type User struct{
	Id uint
	Username string
	Password string
	Nickname string
}

type File struct{
	Id uint
	Uid uint
	Cap string
	Name string
	Username string
	Location string
	Md5  string `json:"-"`
}
type Files []File

type FileShare struct{
	Id uint
	Uid uint
	UserId uint
	Uuid uuid.UUID
	Password string
	Timeout string
	DownloadNum uint
}

//user.db
func (u *User)GetUserByName(username string)bool{
	if DB.Table("user").Where("username = ?",username).First(&u).RecordNotFound(){
		//utils.CheckErr(err,"get the user information failed")
		return  false
	}
	return true
}

func (u *User)GetUser(id uint)bool{
	err:=DB.Table("user").Where("id = ?",id).First(&u).RecordNotFound()
	if err {
		//utils.CheckErr(err, "get the user information failed")
		return false
	}
	return true
}

func (u *User)InsertUser()bool{
	err:=DB.Table("user").Create(&u).Error
	if err!=nil {
		utils.CheckErr(err, "insert the user failed")
		return false
	}
	return true
}

func (u *User)GetUserNickName(nickname string)bool{
	err:=DB.Table("user").Where("nickname = ?",nickname).First(&u).RecordNotFound()
	if err {
		//utils.CheckErr(err, "get the user information failed")
		return false
	}
	return true
}

//file.db
func (f *File)InsertFile()bool{
	err:=DB.Table("file").Create(&f).Error
	if err!=nil {
		utils.CheckErr(err, "insert the file failed")
		return false
	}
	return true
}

func (f *File)UpdateFile(username string,uid uint,id uint)bool{
	err:=DB.Table("file").Where("id = ? AND username = ? ",id,username).Update("uid",uid).RecordNotFound()
	if err {
		return false
	}
	return true
}

func (f *File)GetFile(id uint)bool{
	if DB.Table("file").Where("id = ?",id).First(&f).RecordNotFound(){
		//utils.CheckErr(err,"get the file information failed")
		return false
	}
	return true
}

func (f *Files)GetPersonalFile(id uint)bool{
	if DB.Table("file").Where("uid = ?",id).Find(&f).RecordNotFound(){
		//utils.CheckErr(err,"get the personal file failed")
		return false
	}
	return true
}

func (f *Files)GetPublicFile()bool{
	if DB.Table("file").Where("uid = ?",0).Find(&f).RecordNotFound(){
		//utils.CheckErr(err,"get the all public file failed")
		return false
	}
	return true
}

func (f *Files)GetUserFile(id uint)bool{
	if DB.Table("file").Where("uid = ? OR uid = ?",id,0).Find(&f).RecordNotFound(){
		//utils.CheckErr(err,"get the uer file failed")
		return false
	}
	return true
}

func (f *File)GetFileByFinalPath(name string,location string,uid uint)bool{
	if DB.Table("file").Where("name = ? AND location = ? AND uid = ?",name,location,uid).First(&f).RecordNotFound(){
		//utils.CheckErr(err,"get the file information failed")
		return false
	}
	return true
}

func (f *File)GetFileByMd5(md5 string)bool{
	if DB.Table("file").Where("md5 = ?",md5).First(&f).RecordNotFound(){
		return false
	}
	return true
}


//file_share.db
func (f *FileShare)InsertShare()bool{
	err:=DB.Table("file_share").Create(&f).Error
	if err!=nil {
		utils.CheckErr(err, "insert the file_share failed")
		return false
	}
	return true
}


func (f *FileShare)GetShareFile(uuid uuid.UUID)bool{
	if DB.Table("file_share").Where("uuid = ?",uuid).First(&f).RecordNotFound(){
		//utils.CheckErr(err,"can't get the file")
		return false
	}
	return  true
}

func (f *FileShare)DeleteShareFile()bool{
	if err:=DB.Table("file_share").Delete(&f).Error;err!=nil{
		utils.CheckErr(err,"delete the share file failed")
		return false
	}
	return true
}

func (f *FileShare)AddShareFileNum()bool{
	if err:=DB.Table("file_share").Where("id = ?",f.Id).
		Update("download_num",gorm.Expr("download_num + 1")).Error;err!=nil{
			utils.CheckErr(err,"update the downloadNum failed")
			return  false
	}
		return true
}


